$(function () {
    $('form').parsley();
    getAllPlan();
    dynamicBinding();
});

//遍历所有plan输出到表格
function getAllPlan() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {
            title: "序号", data: null, "render": function (data, type, row, meta) {
                return data = Number(meta.row) + Number(1);
            }
        },
        {title: "名字", data: "name"},
        {title: "备案信息", data: "recordInfo"},
        {title: "预案层级", data: "planLevel"},
        {title: "预案类型", data: "planType"},
        {title: "详细文件", data: "fileUrl", "render": function (data, type, row, meta) {
                let index = data.lastIndexOf("/");
                return '<a href="' + data + '" download="' + data.substring(index + 1, data.length) + '" target="_blank">' + data.substring(index + 1, data.length) + '</a>';
            }
        },
        {
            title: "操作", data: "id", "orderable": false, // 禁用排序
            "defaultContent": "", "render": function (data, type, row, meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updatePlanModal'>更新</button>";
                }
                return data = deleteStr + updateStr;
            }
        }
    ];
    let url = 'plan/getAllPlan';
    dataTableInit(url, columns);
}


//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind( '/plan/deletePlan');
    //为所有的update按钮绑定update事件
    updateBind( '#toUpdatePlan', '/plan/updatePlan', false, false, "#updatePlanModal");
    //为所有的add按钮绑定add事件
    addBind( '#toAddPlan', '/plan/addPlan', false, false, "#addPlanModal");

    //为add模态框绑定消失事件-清空模态框中内容
    $('#addPlanModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddPlan").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updatePlanModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdatePlan").reset();
    });
}
